#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPushButton>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QPushButton *pStartButton = ui->start;
    QPushButton *pStopButton = ui->stop;
    m_pProgressBar = ui->progressBar;
    m_pTimer = new QTimer();

    pStartButton->setText(("开始"));
    pStopButton->setText(("停止"));

    m_pProgressBar->setRange(0, 100);
    m_pProgressBar->setValue(1);

    // 设置超时间隔
    m_pTimer->setInterval(1000);

    // 连接信号槽
    connect(pStartButton, SIGNAL(clicked(bool)), m_pTimer, SLOT(start()));
    connect(pStopButton, SIGNAL(clicked(bool)), m_pTimer, SLOT(stop()));
    connect(m_pTimer, SIGNAL(timeout()), this, SLOT(updateProgress()));
}


// 槽函数
void MainWindow::updateProgress()
{
    // 获取当前进度值，+1
    int nCurrentValue = m_pProgressBar->value();
    nCurrentValue++;
    if (nCurrentValue >= 100)
        m_pTimer->stop();

    // 设置新的进度值
    m_pProgressBar->setValue(nCurrentValue);
}

MainWindow::~MainWindow()
{
    delete ui;
}
